package cn.com.taurus.controller.major;

import cn.com.taurus.bean.*;
import cn.com.taurus.biz.FundManagerWebBeanBiz;
import cn.com.taurus.biz.HomePageWebBeanBiz;
import cn.com.taurus.controller.user.LoginRegisterController;
import cn.com.taurus.controller.user.MyPurseController;
import cn.com.taurus.entity.Fund;
import cn.com.taurus.impl.HomeFundCompanySearchBeanImpl;
import cn.com.taurus.impl.HomeFundManagerSearchBeanImpl;
import cn.com.taurus.impl.HomeFundSearchBeanImpl;
import cn.com.taurus.service.HomePageWebBeanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.*;

import static java.util.stream.Collectors.collectingAndThen;
import static java.util.stream.Collectors.toCollection;

@Controller
public class HomePageWebController {
    @Autowired
    HomePageWebBeanBiz homePageWebBeanBiz;
    @Autowired
    MyPurseController myPurseController;
    @Autowired
    HomeFundSearchBeanImpl fundImpl;
    @Autowired
    HomeFundCompanySearchBeanImpl comImpl;
    @Autowired
    HomeFundManagerSearchBeanImpl manImpl;

    //这样写访问路径才会被springsecurity放行
    @RequestMapping(value = "/Anonymous/homePageWeb")
    public ModelAndView fundManagerWeb(){
        List<HomePageWebBean> list = homePageWebBeanBiz.selectRandomFiveFund();
        for (HomePageWebBean homePageWebBean:list) {
            System.out.println(homePageWebBean.toString());
        }
        ModelAndView modelAndView = new ModelAndView();

        modelAndView.addObject("homePageData",list);
        modelAndView.addObject("myPurseData",myPurseController.getMyPurseWebBean());
        modelAndView.setViewName("major/homepage");
        return modelAndView;
    }

    @RequestMapping(value = "/Anonymous/homePageSearchWeb")
    public ModelAndView search(String searchInput){
        ModelAndView modelAndView = new ModelAndView();
        List<HomeFundSearchBean> FundList = fundImpl.searchFund(searchInput);
        List<HomeFundSearchBean> DateList = fundImpl.searchFvcDate();
        List<HomeFundSearchBean> NewList = new ArrayList<>();
        for (HomeFundSearchBean bean : FundList) {
            for (HomeFundSearchBean bean2 : DateList) {
                if(bean.getfId().equals(bean2.getfId())) {
                    Date date = new Date(bean2.getFvcDate());
                    GregorianCalendar gc = new GregorianCalendar();
                    gc.setTime(date);
                    java.text.SimpleDateFormat format = new java.text.SimpleDateFormat("yyyy-MM-dd");
                    String str =format.format(gc.getTime());
                    bean2.setCreateTime(str);
                    HomeFundSearchBean fund = new HomeFundSearchBean(bean.getfId(),bean.getfName(),bean.getFtName(),bean.getFmName(),bean.getFcName(),str,bean.getDwjz(),bean.getSy1y(),bean.getSy6y(),bean.getSy1n());
                    NewList.add(fund);
                }
            }
        }
        List<HomeFundSearchBean> list = NewList.stream().collect(collectingAndThen(toCollection(()-> new TreeSet<>(Comparator.comparing(HomeFundSearchBean::getfId))),ArrayList::new));
        modelAndView.addObject("FundList",list);
        modelAndView.addObject("FundLength",list.size());
        List<HomeFundCompanySearchBean> CompanyList = comImpl.searchCompany(searchInput);
        modelAndView.addObject("CompanyList",CompanyList);
        modelAndView.addObject("CompanyLength",CompanyList.size());
        List<HomeFundManagerSearchBean> ManagerList = manImpl.searchManager(searchInput);

        modelAndView.addObject("ManagerList",ManagerList);
        modelAndView.addObject("ManagerLength",ManagerList.size());
        modelAndView.addObject("CountLength",list.size()+CompanyList.size()+ManagerList.size());
        modelAndView.setViewName("major/HomeSearch");
        return modelAndView;
    }

}
